//
//  KKPageView.m
//  Butler
//
//  Created by liaozhenming on 16/6/8.
//  Copyright © 2016年 liaozhenming. All rights reserved.
//

#import "KKPageView.h"

#import "KKPageControllerModel.h"

@interface KKPageView()

@property (nonatomic, strong) KKPageControllerModel *pageControllerModel;
@property (nonatomic, strong) NSArray *pageItems;

@end

@implementation KKPageView

- (void)awakeFromNib {
    [super awakeFromNib];
    
    [self init_createPagesView];
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame: frame];
    if (self) {
        [self init_createPagesView];
    }
    return self;
}

#pragma mark - Setter methods

- (void)setPageViewCellType:(NSInteger)pageViewCellType {
    _pageViewCellType = pageViewCellType;
    _pageControllerModel.type = _pageViewCellType;
    [self private_updatePagesViewController];
}

#pragma mark - Init methods
- (void)init_createPagesView {
    
    _pageControllerModel = [KKPageControllerModel new];
    
    UIImageView *backgroundImage = [[UIImageView alloc] initWithFrame: [self bounds]];
    backgroundImage.image = [UIImage imageNamed: @"main_background"];
    backgroundImage.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    backgroundImage.contentMode = UIViewContentModeCenter;
    backgroundImage.center = CGPointMake(CGRectGetWidth(self.bounds)/2 + 3, CGRectGetHeight(self.bounds)/2);
    [self addSubview: backgroundImage];
    
    NSDictionary *options = [NSDictionary dictionaryWithObject: [NSNumber numberWithInt: UIPageViewControllerSpineLocationMid] forKey: UIPageViewControllerOptionSpineLocationKey];
    
    UIPageViewController *pagesViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl navigationOrientation: UIPageViewControllerNavigationOrientationHorizontal options: options];
    pagesViewController.dataSource = self;
    pagesViewController.view.frame = CGRectMake(0, 0, 868, 585);
    pagesViewController.view.center = CGPointMake(CGRectGetWidth(self.bounds)/2, CGRectGetHeight(self.bounds)/2);
    pagesViewController.view.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin |UIViewAutoresizingFlexibleRightMargin| UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;

    [self addSubview: pagesViewController.view];
    _pageViewController = pagesViewController;
    
    [self private_updatePagesViewController];
}

#pragma mark - Private methods

- (void)private_updatePagesViewController {
    
    KKPageChildViewController *firstViewController = [_pageControllerModel viewControllerWithIndex: 0];
    KKPageChildViewController *lastViewController = [_pageControllerModel viewControllerWithIndex: 1];

    [_pageViewController setViewControllers: @[firstViewController, lastViewController] direction:UIPageViewControllerNavigationDirectionForward animated: NO completion: nil];
}

#pragma mark - UIPageViewControllerDelegate methods

- (UIViewController*)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
    
    KKPageChildViewController *childViewController = (KKPageChildViewController*)viewController;
    return [_pageControllerModel viewControllerWithIndex: childViewController.pageIndex + 1];
}

- (UIViewController*)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
    
    KKPageChildViewController *childViewController = (KKPageChildViewController*)viewController;
    
    if (childViewController.pageIndex == 0) {
        return nil;
    }
    return [_pageControllerModel viewControllerWithIndex: childViewController.pageIndex - 1];
}

@end
